const algorithmSelect = document.getElementById('algorithm');
const keyInput = document.getElementById('keyInput');
const decryptBtn = document.getElementById('decryptBtn');

algorithmSelect.addEventListener('change', function() {
    const algorithm = this.value;
    keyInput.style.display = algorithm === 'AES' ? 'block' : 'none';
    decryptBtn.style.display = ['BASE64', 'AES'].includes(algorithm) ? 'block' : 'none';
});

function encrypt() {
    const algorithm = algorithmSelect.value;
    const input = document.getElementById('input').value;
    if (!input) {
        showToast('请输入要加密的内容');
        return;
    }

    let result = '';
    try {
        switch(algorithm) {
            case 'MD5':
                result = CryptoJS.MD5(input).toString();
                break;
            case 'SHA1':
                result = CryptoJS.SHA1(input).toString();
                break;
            case 'SHA256':
                result = CryptoJS.SHA256(input).toString();
                break;
            case 'SHA512':
                result = CryptoJS.SHA512(input).toString();
                break;
            case 'BASE64':
                result = btoa(encodeURIComponent(input));
                break;
            case 'AES':
                const key = document.getElementById('key').value;
                if (!key) {
                    throw new Error('请输入密钥');
                }
                result = CryptoJS.AES.encrypt(input, key).toString();
                break;
        }
        document.getElementById('result').textContent = result;
        showToast('加密成功');
    } catch (error) {
        showToast('错误：' + error.message);
    }
}

function decrypt() {
    const algorithm = algorithmSelect.value;
    const input = document.getElementById('input').value;
    if (!input) {
        showToast('请输入要解密的内容');
        return;
    }

    let result = '';
    try {
        switch(algorithm) {
            case 'BASE64':
                result = decodeURIComponent(atob(input));
                break;
            case 'AES':
                const key = document.getElementById('key').value;
                if (!key) {
                    throw new Error('请输入密钥');
                }
                const bytes = CryptoJS.AES.decrypt(input, key);
                result = bytes.toString(CryptoJS.enc.Utf8);
                if (!result) {
                    throw new Error('解密失败，请检查密钥是否正确');
                }
                break;
            default:
                throw new Error('该算法不支持解密');
        }
        document.getElementById('result').textContent = result;
        showToast('解密成功');
    } catch (error) {
        showToast('解密失败：' + error.message);
        document.getElementById('result').textContent = '';
    }
}

function copyResult() {
    const result = document.getElementById('result').textContent;
    if (!result) {
        showToast('没有可复制的内容');
        return;
    }
    navigator.clipboard.writeText(result).then(() => {
        showToast('结果已复制到剪贴板');
    });
}

function showToast(message) {
    const toast = document.getElementById('toast');
    toast.textContent = message;
    toast.classList.add('show');
    setTimeout(() => {
        toast.classList.remove('show');
    }, 2000);
}

function clearAll() {
    document.getElementById('input').value = '';
    document.getElementById('key').value = '';
    document.getElementById('result').textContent = '';
    showToast('已清空所有内容');
}

window.onload = function() {
    const algorithm = algorithmSelect.value;
    keyInput.style.display = algorithm === 'AES' ? 'block' : 'none';
    decryptBtn.style.display = ['BASE64', 'AES'].includes(algorithm) ? 'block' : 'none';
}; 